package test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main1 {
	public static void main(String[] args) {
		displayAll();
	}
	public static void displayAll() { // 显示所有学生的学号、名字和出生时间，select
		String sql = "select stuno,name,birthdate from student";
		String URL = "jdbc:mysql://localhost:3306/test";
		String driverName = "com.mysql.jdbc.Driver";
		String userName = "root";// root
		String password = "kfc123456";// 123456
		Connection conn = null;

		try {
			Class.forName(driverName);
			conn = DriverManager.getConnection(URL, userName, password);
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			// id | stuno | name | gender | birthdate | major
			while (resultSet.next()) {
				// int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String stuno = resultSet.getString("stuno");
				Date date = resultSet.getDate("birthdate");
				System.out.println(" name=" + name + " stuno=" + stuno + " birthdate=" + date);
			}

		} catch (SQLException | ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // ����ʹ��Try..with..resources�﷨��
			conn = null;
		}
	}
}
